'use strict';
/**
 * login action transcode from ES6
 */
var AppDispatcher = require('../dispatchers/AppDispatcher');
var LoginConstants = require('../constants/login');
var RouterContainer = require('../services/RouterContainer');

module.exports = {
	loginUser: function(jwt)
	{
		var savedJwt = localStorage.getItem('jwt'); // you might have other storage, change it here
		AppDispatcher.dispatch({
			actionType: LoginConstants.LOGIN_USER,
			jwt: jwt
		});
		if (saveJwt!==jwt) {
			var nextPath = RouterContainer.get().getCurrentQuery().nextPath || '/';
			RouterContainer.get().transitionTo(nextPath);
			localStorage.getItem('jwt' , jwt);
		}
	},
	logoutUser: function() {
		RouterContainer.get().transitionTo(LoginConstant.LOCAL_LOGIN_URL); // should this be in the constant?
		localStorage.removeItem('jwt');
		AppDispatcher.dispatch({
			actionType: LoginConstant.LOGOUT_USER
		})
	}
};
